<?php

namespace Home\Model;

use Think\Model;

/**
 * Description of UserModel
 *
 * @author xtceetg
 */
class UserModel extends Model {

    protected $tableName = "user_info";

    public function getByUsername($username) {
        //$sql = 'SELECT u.uid,u.username,u.`password`,u.reg_ip,u.reg_time,u.login_times,u.last_ip,u.last_time,u.register_type,uai.real_name,uai.idCard,uai.email FROM user_info AS u ,user_add_info AS uai WHERE u.uid=uai.uid AND username="' . $username . '"';
        $sql = "select * from user_info WHERE `username`='" . $username . "'";
        $user = $this->query($sql);
        if ($user) {
            return $user[0];
        } else {
            return FALSE;
        }
    }

    public function regex_username($username) {
        return preg_match('/^[a-zA-Z][\w]{5,11}$/', $username);
    }

    public function check_password($password) {
        if (strlen($password) < 6 || strlen($password) > 12) {
            return false;
        } else {
            return true;
        }
    }

    public function compare_password($password, $repassword) {
        if ($password != $repassword) {
            return FALSE;
        } else {
            return true;
        }
    }

    public function check_realname($realname) {
        if (strlen($realname) > 20 || strlen($realname) < 2 || !preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $realname)) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * 暂时不用
     */

    public function check_idCard($idCard) {
        if (!preg_match('/^([\d]{15}|[\d]{14}[\dx]|[\d]{18}|[\d]{17}[\dx])$/i', $idCard)) {
            return array(
                'ret' => 3,
                'msg' => '身份号不正确'
            );
        } else {
            if (strlen($idCard) == 15) {
                $prefix = '19';
                $year_m_d = str_split(substr($idCard, 6, 12), 2);
            } else if (strlen($idCard) == 18) {
                $prefix = substr($idCard, 6, 2);
                $year_m_d = str_split(substr($idCard, 8, 14), 2);
            }
            $year = $prefix . $year_m_d[0];
            if (checkdate($year_m_d[1], $year_m_d[2], $year)) {
                return array($year, $year_m_d[1], $year_m_d[2]);
            } else {
                return false;
            }
        }
    }

    public function login_out() {
        session('[start]');
        cookie('webgame', null);
        cookie('last_game', null);
        $_SESSION['userinfo'] = null;
        unset($_SESSION['userinfo']);
        return true;
    }

    public function check_login() {
        session('[start]');
        $userinfo = session("userinfo");
        file_put_contents("/home/wwwroot/www.248game.com/Application/Runtime/Logs/login.txt", var_export($userinfo,true));
        //var_dump(isset($userinfo));exit;//false;未登录
        if (isset($userinfo)) {
            $this->set_login_cookie();
            return $userinfo['uid'];
        } else {
            $this->login_out();
            return -1;
        }
    }

    public function set_login_cookie() {
        session('[start]');
        $userinfo = session("userinfo");
        //cookie('webgame', $userinfo['username'], 0);
        setcookie('webgame', $userinfo['username'], time() + 3600, '/', '248game.com');
        $lastgame = new \Home\Model\GameUserRelationModel();
        $lastgame->set_last_game_cookie($userinfo['uid']);
    }

    public function update_password($userinfo = array(), $password) {
        $data['password'] = $password;
        return $this->where("uid='%d' and username='%s'", array($userinfo['uid'], $userinfo['username']))->save($data);
    }

    /**
     * 记录用户登录次数，时间和ip
     * @param type $user
     */
    public function set_login_info($user) {
        //$sql = "update " . $this->tableName . " set login_times=login_times+1,last_ip='" . get_client_ip(0, true) . "',last_time='" . time() . "' where uid='" . $user['uid'] . "' and username='" . $user['username'] . "'";        
        //var_dump($this->query($sql));exit;
        $data['login_times'] = $user['login_times'] + 1;
        $data['last_ip'] = get_client_ip(0, true);
        $data['last_time'] = time();
        $this->where("uid='%d' and username='%s'", array($user['uid'], $user['username']))->save($data);
    }

}
